package com.lanhai.javaBean;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.pool.DruidDataSource;
import com.lanhai.dao.DBHelper;
import com.lanhai.model.Guidepost;

public class GuidepostBean {

	/**
	 * 加载地标列表
	 */
	public static List<Guidepost> loadData(DruidDataSource dataSource){
		List<Guidepost> guidepostList = new ArrayList<Guidepost>();
		guidepostList = loadData4Sqlite(dataSource);
		return guidepostList;
	}
	
	public static List<Guidepost> loadData4Sqlite(DruidDataSource dataSource){
		List<Guidepost> guidepostList = new ArrayList<Guidepost>();
		String sql = "select * from guidepost";
		DBHelper conn = new DBHelper();
		try {
			ResultSet resultSet = conn.executeQuery(sql, dataSource);
			while (resultSet.next()) {
				Guidepost gd = new Guidepost();
				gd.setId(resultSet.getInt("id"));
				gd.setName(resultSet.getString("name"));//名称
				gd.setNum(resultSet.getInt("num"));//地标号
				gd.setType(resultSet.getInt("type"));//类型
				gd.setSubType(resultSet.getInt("subtype"));//子类
				gd.setAgvState(resultSet.getInt("agvstate"));//对应的AGV状态
				gd.setNextNum(resultSet.getInt("nextNum"));
				gd.setCaId(resultSet.getInt("caId"));//主交管区ID
				gd.setCtrlIp(resultSet.getString("ctrlIp"));//所属调度的IP地址
				gd.setStep(resultSet.getInt("step"));//工序
				gd.setNextStep(resultSet.getInt("nextStep"));//下一道工序
				String cdIds = resultSet.getString("controlAreaIds");//对应交管区ID
				if(cdIds != null){
					String[] cdIdArr = cdIds.split(",");
					if(cdIdArr.length > 0){
						int[] cids = new int[cdIdArr.length];
						for(int i = 0; i < cdIdArr.length; i++){
							if(!cdIdArr[i].equals("")){
								cids[i] = Integer.parseInt(cdIdArr[i]);
							}
						}
						gd.setControlAreaIds(cids);
					}
				}
				String acaIds = resultSet.getString("aheadCaIds");//前方交管区ID
				if(acaIds != null){
					String[] acaIdArr = acaIds.split(",");
					if(acaIdArr.length > 0){
						int[] cids = new int[acaIdArr.length];
						for(int i = 0; i < acaIdArr.length; i++){
							cids[i] = Integer.parseInt(acaIdArr[i]);
						}
						gd.setAheadCaIds(cids);
					}
				}
				guidepostList.add(gd);				
			}
			resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return guidepostList;
	}
	
}
